<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10 0010
 * Time: 10:43
 */

namespace app\admin\controller;


use think\Request;

class Subscription extends Base
{
    protected $_Subscription;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Subscription = new \app\model\model\Subscription();
    }
    public function lists(){
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        $type = input('type');
        if($name){
            $where['email'] = ['like',"%$name%"];
        }
        if($type){
            $where['validity'] = ['like',"%$type%"];
        }
        $data = $this->_Subscription->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            switch ($v['validity'])
            {
                case 1:
                    $v['validity']="是";
                    break;
                case 2:
                    $v['validity']="否";
                    break;
                default:
                    $v['validity']="无效";
            }
            switch ($v['laiyuan'])
            {
                case 1:
                    $v['laiyuan']="PC站";
                    break;
                case 2:
                    $v['laiyuan']="手机站";
                    break;
                default:
                    $v['laiyuan']="未知";
            }

        }
        $list = $data->toArray();
        echo json_echo($list);
    }

    /**
     * 审核
     * */
    public function audit(){

        $re = $this->_Subscription->up(['id'=>input('id')],['validity'=>input('validity')]);
        if($re){
            return json_echo_msg(200,'成功');
        }else{
            return json_echo_msg(500,'失败');
        }
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{

            $data = [
                'email'=>input('email'),

                'create_time'=>time(),
            ];
            $re = $this->_Subscription->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }

    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Subscription->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
        $re = $this->_Subscription->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
        $re = $this->_Subscription->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
}